package com.xiaoshuidi.cloud.module.member.xxljob;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.member.enums.coupon.CouponUsageStatusEnum;
import com.xiaoshuidi.cloud.module.member.mapper.coupon.CouponCustomerMapper;
import com.xiaoshuidi.cloud.module.member.pojo.coupon.CouponCustomerEntity;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class MemberCouponCustomerJob {

    @Autowired
    CouponCustomerMapper couponCustomerMapper;

    /**
     * 过期跑批
     */
    @TenantIgnore
    @XxlJob("member-scanner-customer-expire-job")
    @Transactional
    public void execute() {
        QueryWrapper<CouponCustomerEntity> qw = new QueryWrapper<>();
        qw.eq("usage_status", 1);
        List<CouponCustomerEntity> list = couponCustomerMapper.selectList(qw);
        List<CouponCustomerEntity> listEntity = new ArrayList<>();
        for (CouponCustomerEntity couponCustomerEntity : list) {
            Date currentDate = new Date();
            Date expirationEndTime = couponCustomerEntity.getExpirationEndTime();
            if (expirationEndTime.before(currentDate)) {
                listEntity.add(couponCustomerEntity);
            }
        }
        if (CollectionUtil.isNotEmpty(listEntity)) {
            couponCustomerMapper.updateUsageStatus(listEntity);
        }


    }

    /**
     * 锁定跑批
     */
    @TenantIgnore
    @XxlJob("member-scanner-customer-lock-job")
    @Transactional
    public void task(){
        QueryWrapper<CouponCustomerEntity> qw =new QueryWrapper<>();
        qw.eq("is_locked",true);
        qw.eq("usage_status", CouponUsageStatusEnum.TO_BE_USED.getCode());
        List<CouponCustomerEntity> list = couponCustomerMapper.selectList(qw);
        List<CouponCustomerEntity> listEntity = new ArrayList<>();
        for (CouponCustomerEntity couponCustomerEntity : list) {
            Date lockedTime = couponCustomerEntity.getLockedTime();
            Date currentDate = new Date();
            // 1. 获取时间戳
            long lockedTimestamp = lockedTime.getTime();
            long currentTimestamp = currentDate.getTime();
            // 2. 计算时间差（毫秒）
            long timeDiffInMillis = currentTimestamp - lockedTimestamp;
            // 3. 将时间差转换为分钟数
            int minutes = (int) (timeDiffInMillis / (1000 * 60));
            int lockedDuration = couponCustomerEntity.getLockedDuration();
            if (minutes > lockedDuration) {
                listEntity.add(couponCustomerEntity);
            }
        }
        if (CollectionUtil.isNotEmpty(listEntity)) {
            couponCustomerMapper.updateIsLocked(listEntity);
        }


    }


}
